package com.hp.it.server.servlet;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hp.it.pool.db.ConnectionPool;
import com.hp.it.server.configuration.AggregateConstant;
import com.hp.it.server.configuration.ReportConstant;
import com.hp.it.server.context.DatabasePoolContext;
import com.hp.it.server.context.ProjectContext;
import com.hp.it.sonar.bean.Violation;
import com.hp.it.sonar.service.IViolationService;
import com.hp.it.sonar.service.impl.ViolationService;
import com.hp.it.version.bean.LogEntry;

public class AggregateViolationChangeReportServlet extends HttpServlet
{

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
	{
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
	{
		String aggregateName = req.getParameter("Aggregate");
	}

	static class AggregateReportTask implements Runnable
	{
		private String kee;

		private int period;

		private String violationPriority;

		private Map<String, Map<String, String>> summary;

		Map<String, Collection<Violation>> violationDetails;

		Map<String, LogEntry> versionDetails;

		public AggregateReportTask(String kee, int period, String violationPriority)
		{
			this.kee = kee;
			this.period = period;
			this.violationPriority = violationPriority;
			summary = new HashMap<String, Map<String, String>>();
			violationDetails = new HashMap<String, Collection<Violation>>();
			versionDetails = new HashMap<String, LogEntry>();
		}

		public void run()
		{
			generateAndSendEmail();
		}

		public boolean generateAndSendEmail()
		{
			Properties properties = ProjectContext.getProperties(kee);
			if (properties == null)
			{
				// TODO
				// properties not found
				return false;
			}
			if (period == 0)
			{
				period = Integer.valueOf(properties.getProperty(AggregateConstant.DEFAULT_PERIOD));
			}
			if (violationPriority == null || "".equalsIgnoreCase(violationPriority))
			{
				violationPriority = properties.getProperty(AggregateConstant.DEFAULT_PRIORITY);
			}
			retrieveAggregateViolationData(properties.getProperty(AggregateConstant.AGGREGATE_PROJECTS), period,
					violationPriority);
			return false;
		}

		private void retrieveAggregateViolationData(String aggregateProjects, int period, String violationPriority)
		{
			for (String projectKee : aggregateProjects.split("\\*"))
			{
				/*
				 * ViolationSumary
				 */

				/*
				 * Violation details
				 */
				// Retrieve the violation information
				Map<String, Collection<Violation>> keeViolation = retrieveViolationsByPriority(projectKee, period,
						violationPriority);
				// Merge the violation details
				mergeViolationDetails(this.violationDetails, keeViolation);
				/*
				 * Version data info
				 */
				// Merge the violation data info
			}
		}

		private Map<String, Collection<Violation>> retrieveViolationsByPriority(String kee, int period,
				String violationPriority)
		{
			Properties properties = ProjectContext.getProperties(kee);

			String[] id = kee.split(":");

			ConnectionPool cp = DatabasePoolContext.getInstance(id[0] + ":" + id[1]);

			IViolationService violationService = new ViolationService(cp,
					properties.getProperty(ReportConstant.PORTAL_URL));

			Map<String, Collection<Violation>> violationDetails = violationService.retrieveViolationDetails(id[0],
					id[1], period, violationPriority);
			return violationDetails;
		}

		private void mergeViolationDetails(Map<String, Collection<Violation>> aggregate,
				Map<String, Collection<Violation>> eachOne)
		{
			Collection<Violation> aggregateBlockerViolationList = aggregate.get("BLOCKER");
			Collection<Violation> artifactBlockerViolationList = aggregate.get("BLOCKER");
			if (artifactBlockerViolationList != null && artifactBlockerViolationList.size() != 0)
			{
				for (Violation violation : artifactBlockerViolationList)
				{
					if (aggregateBlockerViolationList.contains(violation))
					{
						for (Violation original : aggregateBlockerViolationList)
						{
							if (original.equals(violation))
							{
								original.setDelta(original.getDelta() + violation.getDelta());
								original.getProjects().addAll(violation.getProjects());
							}
						}
					} else
					{
						aggregateBlockerViolationList.add(violation);
					}

				}
			}

			Collection<Violation> aggregateCriticalViolationList = aggregate.get("CRITICAL");
			Collection<Violation> artifactCriticalViolationList = aggregate.get("CRITICAL");
			if (artifactCriticalViolationList != null && artifactCriticalViolationList.size() != 0)
			{
				for (Violation violation : artifactCriticalViolationList)
				{
					if (aggregateCriticalViolationList.contains(violation))
					{
						for (Violation original : aggregateCriticalViolationList)
						{
							if (original.equals(violation))
							{
								original.setDelta(original.getDelta() + violation.getDelta());
								original.getProjects().addAll(violation.getProjects());
							}
						}
					} else
					{
						aggregateCriticalViolationList.add(violation);
					}

				}
			}

			Collection<Violation> aggregateMajorViolationList = aggregate.get("MAJOR");
			Collection<Violation> artifactMajorViolationList = aggregate.get("MAJOR");
			if (artifactMajorViolationList != null && artifactMajorViolationList.size() != 0)
			{
				for (Violation violation : artifactMajorViolationList)
				{
					if (aggregateMajorViolationList.contains(violation))
					{
						for (Violation original : aggregateMajorViolationList)
						{
							if (original.equals(violation))
							{
								original.setDelta(original.getDelta() + violation.getDelta());
								original.getProjects().addAll(violation.getProjects());
							}
						}
					} else
					{
						aggregateMajorViolationList.add(violation);
					}

				}
			}

			Collection<Violation> aggregateMinorViolationList = aggregate.get("MINOR");
			Collection<Violation> artifactMinorViolationList = aggregate.get("MINOR");
			if (artifactMinorViolationList != null && artifactMinorViolationList.size() != 0)
			{
				for (Violation violation : artifactMinorViolationList)
				{
					if (aggregateMinorViolationList.contains(violation))
					{
						for (Violation original : aggregateMinorViolationList)
						{
							if (original.equals(violation))
							{
								original.setDelta(original.getDelta() + violation.getDelta());
								original.getProjects().addAll(violation.getProjects());
							}
						}
					} else
					{
						aggregateMinorViolationList.add(violation);
					}

				}
			}

			Collection<Violation> aggregateInfoViolationList = aggregate.get("INFO");
			Collection<Violation> artifactInfoViolationList = aggregate.get("INFO");
			if (artifactInfoViolationList != null && artifactInfoViolationList.size() != 0)
			{
				for (Violation violation : artifactInfoViolationList)
				{
					if (aggregateInfoViolationList.contains(violation))
					{
						for (Violation original : aggregateInfoViolationList)
						{
							if (original.equals(violation))
							{
								original.setDelta(original.getDelta() + violation.getDelta());
								original.getProjects().addAll(violation.getProjects());
							}
						}
					} else
					{
						aggregateInfoViolationList.add(violation);
					}

				}
			}
		}
	}

}
